package com.nidu.demo.mail.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.mail.convertor.MailTemplateConvertor;
import com.nidu.demo.mail.convertor.MailTemplateCriteriaConvertor;
import com.nidu.demo.mail.dto.MailTemplateCO;
import com.nidu.demo.mail.dto.MailTemplatePageQry;
import com.nidu.demo.mail.gateway.MailTemplateGateway;
import com.nidu.demo.mail.model.MailTemplate;
import com.nidu.demo.mail.model.MailTemplateCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 邮件模板分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MailTemplatePageQryExe {

    private final MailTemplateGateway gateway;
    private final MailTemplateConvertor convertor;
    private final MailTemplateCriteriaConvertor criteriaConvertor;

    public PageResponse<MailTemplateCO> execute(MailTemplatePageQry qry) {
        // 1. 转换查询条件
        MailTemplateCriteria criteria = criteriaConvertor.toDataObject(qry);

        // 2. 执行分页查询
        PageResponse<MailTemplate> page = gateway.pageByCondition(criteria);

        // 3. 转换为DTO并返回
        List<MailTemplateCO> mailTemplateCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(mailTemplateCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

}
